package com.ehouse.database;

import java.util.HashMap;
import java.util.Set;
import java.util.Map.Entry;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;

import com.ehouse.provider.MainContentProvider;


public class WindowTable {
//	// Database table
		public static final String NAME = "windows";
		public static final String COLUMN_ID = "_id";
		public static final String COLUMN_LABEL = "label";
		public static final String COLUMN_STATE = "state";

		public static final Uri CONTENT_URI = Uri.parse(MainContentProvider.BASE_URI.toString() + NAME);
		
		// Database creation SQL statement
		public static final String DATABASE_CREATE = "create table " 
				+ NAME
				+ "(" + COLUMN_ID + " integer primary key autoincrement, " 
				+ COLUMN_LABEL + " text not null, " 
				+ COLUMN_STATE + " integer not null " + ");";
		
		public static void update(ContentResolver cr, HashMap<String, Boolean> windowItems){
			Set<Entry<String, Boolean>> set = windowItems.entrySet();
		    for(Entry<String, Boolean> entry : set){
		    	String where = WindowTable.COLUMN_LABEL+" = '"+entry.getKey()+"'";
		    	String[] projection = new String[]{WindowTable.COLUMN_ID, WindowTable.COLUMN_LABEL, WindowTable.COLUMN_STATE};
			  Cursor query = cr.query(WindowTable.CONTENT_URI, projection, where, null, null);

	  	      ContentValues values = new ContentValues();

	   	      values.put(WindowTable.COLUMN_LABEL, entry.getKey());
	   	      values.put(WindowTable.COLUMN_STATE, entry.getValue());   
	   
		   	   if(query.getCount() ==0)
		   	      cr.insert(WindowTable.CONTENT_URI, values);
		   	   else{
		   		   cr.update(WindowTable.CONTENT_URI, values, where, null);
		   	   }
		   	query.close();
		    }
		    
		}
}
